if (typeof jQuery === "undefined") { throw new Error("jQuery plugins need to be before this file"); }
$.AdminBSB = {};
$.AdminBSB.options = {
    colors: {
        red: '#F44336',
        pink: '#E91E63',
        purple: '#9C27B0',
        deepPurple: '#673AB7',
        indigo: '#3F51B5',
        blue: '#2196F3',
        lightBlue: '#03A9F4',
        cyan: '#00BCD4',
        teal: '#009688',
        green: '#4CAF50',
        lightGreen: '#8BC34A',
        lime: '#CDDC39',
        yellow: '#ffe821',
        amber: '#FFC107',
        orange: '#FF9800',
        deepOrange: '#FF5722',
        brown: '#795548',
        grey: '#9E9E9E',
        blueGrey: '#607D8B',
        black: '#000000',
        white: '#ffffff'
    },
    leftSideBar: {
        scrollColor: 'rgba(0,0,0,0.5)',
        scrollWidth: '4px',
        scrollAlwaysVisible: false,
        scrollBorderRadius: '0',
        scrollRailBorderRadius: '0'
    },
    dropdownMenu: {
        effectIn: 'fadeIn',
        effectOut: 'fadeOut'
    }
}
/*
 * Left Sidebar
 */
$.AdminBSB.leftSideBar = {
    activate: function() {
        var _this = this;
        var $body = $('body');
        var $overlay = $('.overlay');
        // Close sidebar
        $(window).click(function(e) {
            var $target = $(e.target);
            if (e.target.nodeName.toLowerCase() === 'i') {
                $target = $(e.target).parent();
            }
            if (!$target.hasClass('bars') && _this.isOpen() && $target.parents('#leftsidebar').length === 0) {
                if (!$target.hasClass('js-right-sidebar')) $overlay.fadeOut();
                $body.removeClass('overlay-open');
            }
        });
        $.each($('.menu-toggle.toggled'), function(i, val) {
            $(val).next().slideToggle(0);
        });
        // When page load
        $.each($('.menu .list li.active'), function(i, val) {
            var $activeAnchors = $(val).find('a:eq(0)');
            $activeAnchors.addClass('toggled');
            $activeAnchors.next().show();
        });
        // Collapse or Expand Menu
        $('.menu-toggle').on('click', function(e) {
            var $this = $(this);
            var $content = $this.next();
            if ($($this.parents('ul')[0]).hasClass('list')) {
                var $not = $(e.target).hasClass('menu-toggle') ? e.target : $(e.target).parents('.menu-toggle');
                $.each($('.menu-toggle.toggled').not($not).next(), function(i, val) {
                    if ($(val).is(':visible')) {
                        $(val).prev().toggleClass('toggled');
                        $(val).slideUp();
                    }
                });
            }
            $this.toggleClass('toggled');
            $content.slideToggle(320);
        });
        // Set menu height
        _this.setMenuHeight();
        _this.checkStatuForResize(true);
        $(window).resize(function() {
            _this.setMenuHeight();
            _this.checkStatuForResize(false);
        });
        Waves.attach('.menu .list a', [
            'waves-block'
        ]);
        Waves.init();
    },
    setMenuHeight: function() {
        if (typeof $.fn.slimScroll != 'undefined') {
            var configs = $.AdminBSB.options.leftSideBar;
            var height = ($(window).height() - ($('.legal').outerHeight() + $('.user-info').outerHeight() + $('.navbar').innerHeight()));
            var $el = $('.list');
            $el.slimScroll({
                destroy: true
            }).height("auto");
            $el.parent().find('.slimScrollBar, .slimScrollRail').remove();
            $el.slimscroll({
                height: height + "px",
                color: configs.scrollColor,
                size: configs.scrollWidth,
                alwaysVisible: configs.scrollAlwaysVisible,
                borderRadius: configs.scrollBorderRadius,
                railBorderRadius: configs.scrollRailBorderRadius
            });
        }
    },
    checkStatuForResize: function(firstTime) {
        var $body = $('body');
        var $openCloseBar = $('.navbar .navbar-header .bars');
        var width = $body.width();
        if (firstTime) {
            $body.find('.content, .sidebar').addClass('no-animate').delay(1000).queue(function() {
                $(this).removeClass('no-animate').dequeue();
            });
        }
        if (width < 1170) {
            $body.addClass('ls-closed');
            $openCloseBar.fadeIn();
        } else {
            $body.removeClass('ls-closed');
            $openCloseBar.fadeOut();
        }
    },
    isOpen: function() {
        return $('body').hasClass('overlay-open');
    }
};
/*
 * Right Sidebar
 */
$.AdminBSB.rightSideBar = {
    activate: function() {
        var _this = this;
        var $sidebar = $('#rightsidebar');
        var $overlay = $('.overlay');
        // Close sidebar
        $(window).click(function(e) {
            var $target = $(e.target);
            if (e.target.nodeName.toLowerCase() === 'i') {
                $target = $(e.target).parent();
            }
            if (!$target.hasClass('js-right-sidebar') && _this.isOpen() && $target.parents('#rightsidebar').length === 0) {
                if (!$target.hasClass('bars')) $overlay.fadeOut();
                $sidebar.removeClass('open');
            }
        });
        $('.js-right-sidebar').on('click', function() {
            $sidebar.toggleClass('open');
            if (_this.isOpen()) {
                $overlay.fadeIn();
            } else {
                $overlay.fadeOut();
            }
        });
    },
    isOpen: function() {
        return $('.right-sidebar').hasClass('open');
    }
}
/*
 * Searchbar
 */
var $searchBar = $('.search-bar');
$.AdminBSB.search = {
    activate: function() {
        var _this = this;
        // Search button click event
        $('.js-search').on('click', function() {
            _this.showSearchBar();
        });
        // Close search click event
        $searchBar.find('.close-search').on('click', function() {
            _this.hideSearchBar();
        });
        // ESC key on pressed
        $searchBar.find('input[type="text"]').on('keyup', function(e) {
            if (e.keyCode == 27) {
                _this.hideSearchBar();
            }
        });
    },
    showSearchBar: function() {
        $searchBar.addClass('open');
        $searchBar.find('input[type="text"]').focus();
    },
    hideSearchBar: function() {
        $searchBar.removeClass('open');
        $searchBar.find('input[type="text"]').val('');
    }
}
/*
 * Navbar
 */
$.AdminBSB.navbar = {
    activate: function() {
        var $body = $('body');
        var $overlay = $('.overlay');
        // Open left sidebar panel
        $('.bars').on('click', function() {
            $body.toggleClass('overlay-open');
            if ($body.hasClass('overlay-open')) {
                $overlay.fadeIn();
            } else {
                $overlay.fadeOut();
            }
        });
        // Close collapse bar on click event
        $('.nav [data-close="true"]').on('click', function() {
            var isVisible = $('.navbar-toggle').is(':visible');
            var $navbarCollapse = $('.navbar-collapse');
            if (isVisible) {
                $navbarCollapse.slideUp(function() {
                    $navbarCollapse.removeClass('in').removeAttr('style');
                });
            }
        });
    }
}
/*
 * Input
 */
$.AdminBSB.input = {
    activate: function() {
        // On focus event
        $('.form-control').focus(function() {
            $(this).parent().addClass('focused');
        });
        // On focusout event
        $('.form-control').focusout(function() {
            var $this = $(this);
            if ($this.parents('.form-group').hasClass('form-float')) {
                if ($this.val() == '') {
                    $this.parents('.form-line').removeClass('focused');
                }
            } else {
                $this.parents('.form-line').removeClass('focused');
            }
        });
        // On label click
        $('body').on('click', '.form-float .form-line .form-label', function() {
            $(this).parent().find('input').focus();
        });
    }
}
/*
 * Form - Select
 */
$.AdminBSB.select = {
    activate: function() {
        if ($.fn.selectpicker) {
            $('select:not(.ms)').selectpicker();
        }
    }
}
/*
 * DropdownMenu
 */
$.AdminBSB.dropdownMenu = {
    activate: function() {
        var _this = this;
        $('.dropdown, .dropup, .btn-group').on({
            "show.bs.dropdown": function() {
                var dropdown = _this.dropdownEffect(this);
                _this.dropdownEffectStart(dropdown, dropdown.effectIn);
            },
            "shown.bs.dropdown": function() {
                var dropdown = _this.dropdownEffect(this);
                if (dropdown.effectIn && dropdown.effectOut) {
                    _this.dropdownEffectEnd(dropdown, function() {});
                }
            },
            "hide.bs.dropdown": function(e) {
                var dropdown = _this.dropdownEffect(this);
                if (dropdown.effectOut) {
                    e.preventDefault();
                    _this.dropdownEffectStart(dropdown, dropdown.effectOut);
                    _this.dropdownEffectEnd(dropdown, function() {
                        dropdown.dropdown.removeClass('open');
                    });
                }
            }
        });
        // Set Waves
        Waves.attach('.dropdown-menu li a', [
            'waves-block'
        ]);
        Waves.init();
    },
    dropdownEffect: function(target) {
        var effectIn = $.AdminBSB.options.dropdownMenu.effectIn, effectOut = $.AdminBSB.options.dropdownMenu.effectOut;
        var dropdown = $(target), dropdownMenu = $('.dropdown-menu', target);
        if (dropdown.size() > 0) {
            var udEffectIn = dropdown.data('effect-in');
            var udEffectOut = dropdown.data('effect-out');
            if (udEffectIn !== undefined) {
                effectIn = udEffectIn;
            }
            if (udEffectOut !== undefined) {
                effectOut = udEffectOut;
            }
        }
        return {
            target: target,
            dropdown: dropdown,
            dropdownMenu: dropdownMenu,
            effectIn: effectIn,
            effectOut: effectOut
        };
    },
    dropdownEffectStart: function(data, effectToStart) {
        if (effectToStart) {
            data.dropdown.addClass('dropdown-animating');
            data.dropdownMenu.addClass('animated dropdown-animated');
            data.dropdownMenu.addClass(effectToStart);
        }
    },
    dropdownEffectEnd: function(data, callback) {
        var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
        data.dropdown.one(animationEnd, function() {
            data.dropdown.removeClass('dropdown-animating');
            data.dropdownMenu.removeClass('animated dropdown-animated');
            data.dropdownMenu.removeClass(data.effectIn);
            data.dropdownMenu.removeClass(data.effectOut);
            if (typeof callback == 'function') {
                callback();
            }
        });
    }
}
/*
 * Browser
 */
var edge = 'Microsoft Edge';
var ie10 = 'Internet Explorer 10';
var ie11 = 'Internet Explorer 11';
var opera = 'Opera';
var firefox = 'Mozilla Firefox';
var chrome = 'Google Chrome';
var safari = 'Safari';
$.AdminBSB.browser = {
    activate: function() {
        var _this = this;
        var className = _this.getClassName();
        if (className !== '') $('html').addClass(_this.getClassName());
    },
    getBrowser: function() {
        var userAgent = navigator.userAgent.toLowerCase();
        if (/edge/i.test(userAgent)) {
            return edge;
        } else if (/rv:11/i.test(userAgent)) {
            return ie11;
        } else if (/msie 10/i.test(userAgent)) {
            return ie10;
        } else if (/opr/i.test(userAgent)) {
            return opera;
        } else if (/chrome/i.test(userAgent)) {
            return chrome;
        } else if (/firefox/i.test(userAgent)) {
            return firefox;
        } else if (!!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/)) { return safari; }
        return undefined;
    },
    getClassName: function() {
        var browser = this.getBrowser();
        if (browser === edge) {
            return 'edge';
        } else if (browser === ie11) {
            return 'ie11';
        } else if (browser === ie10) {
            return 'ie10';
        } else if (browser === opera) {
            return 'opera';
        } else if (browser === chrome) {
            return 'chrome';
        } else if (browser === firefox) {
            return 'firefox';
        } else if (browser === safari) {
            return 'safari';
        } else {
            return '';
        }
    }
}
// Skin changer
var skinChanger = function() {
    $('.right-sidebar .demo-choose-skin li').on('click', function() {
        var $body = $('body');
        var $this = $(this);
        var existTheme = $('.right-sidebar .demo-choose-skin li.active').data('theme');
        $('.right-sidebar .demo-choose-skin li').removeClass('active');
        $this.addClass('active');
        $body.attr('class', 'theme-' + $this.data('theme'));
    });
}
// Skin tab content set height and show scroll
var setSkinListHeightAndScroll = function() {
    var height = $(window).height() - ($('.navbar').innerHeight() + $('.right-sidebar .nav-tabs').outerHeight());
    var $el = $('.demo-choose-skin');
    $el.slimScroll({
        destroy: true
    }).height('auto');
    $el.parent().find('.slimScrollBar, .slimScrollRail').remove();
    $el.slimscroll({
        height: height + 'px',
        color: 'rgba(0,0,0,0.5)',
        size: '4px',
        alwaysVisible: false,
        borderRadius: '0',
        railBorderRadius: '0'
    });
}
// Setting tab content set height and show scroll
var setSettingListHeightAndScroll = function() {
    var height = $(window).height() - ($('.navbar').innerHeight() + $('.right-sidebar .nav-tabs').outerHeight());
    var $el = $('.right-sidebar .demo-settings');
    $el.slimScroll({
        destroy: true
    }).height('auto');
    $el.parent().find('.slimScrollBar, .slimScrollRail').remove();
    $el.slimscroll({
        height: height + 'px',
        color: 'rgba(0,0,0,0.5)',
        size: '4px',
        alwaysVisible: false,
        borderRadius: '0',
        railBorderRadius: '0'
    });
}
// Activate notification and task dropdown on top right menu
var activateNotificationAndTasksScroll = function() {
    $('.navbar-right .dropdown-menu .body .menu').slimscroll({
        height: '254px',
        color: 'rgba(0,0,0,0.5)',
        size: '4px',
        alwaysVisible: false,
        borderRadius: '0',
        railBorderRadius: '0'
    });
}
// 页面加载事件
var addLoadEvent = function(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}
// Google Analiytics
var trackingId = 'UA-30038099-6';
var loadTracking = function() {
    (function(i, s, o, g, r, a, m) {
        i['GoogleAnalyticsObject'] = r;
        i[r] = i[r] || function() {
            (i[r].q = i[r].q || []).push(arguments)
        }, i[r].l = 1 * new Date();
        a = s.createElement(o), m = s.getElementsByTagName(o)[0];
        a.async = 1;
        a.src = g;
        m.parentNode.insertBefore(a, m)
    })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
    ga('create', trackingId, 'auto');
    ga('send', 'pageview');
}
// addLoadEvent(loadTracking);
// ==========================================================================================================================
var hexToRgb = function(hexCode) {
    var patt = /^#([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})$/;
    var matches = patt.exec(hexCode);
    var rgb = "rgb(" + parseInt(matches[1], 16) + "," + parseInt(matches[2], 16) + "," + parseInt(matches[3], 16) + ")";
    return rgb;
}
var hexToRgba = function(hexCode, opacity) {
    var patt = /^#([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})$/;
    var matches = patt.exec(hexCode);
    var rgb = "rgba(" + parseInt(matches[1], 16) + "," + parseInt(matches[2], 16) + "," + parseInt(matches[3], 16) + "," + opacity + ")";
    return rgb;
}
var showNotification = function(colorName, text, placementFrom, placementAlign, animateEnter, animateExit, func) {
    if (colorName === null || colorName === '') {
        colorName = 'bg-black';
    }
    if (text === null || text === '') {
        text = '&nbsp;';
    }
    if (animateEnter === null || animateEnter === '') {
        animateEnter = 'animated fadeInDown';
    }
    if (animateExit === null || animateExit === '') {
        animateExit = 'animated fadeOutUp';
    }
    $.notify({
        message: text
    }, {
        type: colorName,
        placement: {
            from: placementFrom,
            align: placementAlign
        },
        animate: {
            exit: animateExit,
            enter: animateEnter,
        },
        timer: 1000,
        delay: 1200,
        newest_on_top: true,
        allow_dismiss: false,
        showProgressbar: true,
        template: '<div data-notify="container" class="bootstrap-notify-container alert alert-dismissible {0}" role="alert">\
            <button type="button" aria-hidden="true" class="close" data-notify="dismiss">×</button>\
            <span data-notify="icon"></span>\
            <span data-notify="title">{1}</span>\
            <span data-notify="message">{2}</span>\
            <div class="progress" data-notify="progressbar">\
            <div class="progress-bar progress-bar-{0}" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div>\
            </div>\
            <a href="{3}" target="{4}" data-notify="url"></a>\
            </div>',
        onClosed: function() {
            if (typeof func == 'function') func();
        }
    });
}
// ==========================================================================================================================
$(function() {
    $.AdminBSB.browser.activate();
    $.AdminBSB.leftSideBar.activate();
    $.AdminBSB.rightSideBar.activate();
    $.AdminBSB.navbar.activate();
    $.AdminBSB.dropdownMenu.activate();
    $.AdminBSB.input.activate();
    $.AdminBSB.select.activate();
    $.AdminBSB.search.activate();
    setTimeout(function() {
        $('.page-loader-wrapper').fadeOut();
        skinChanger();
        activateNotificationAndTasksScroll();
        setSkinListHeightAndScroll();
        setSettingListHeightAndScroll();
        $(window).resize(function() {
            setSkinListHeightAndScroll();
            setSettingListHeightAndScroll();
        });
    }, 50);
});